package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.*;
import com.cfets.annualAssessment.mapper.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by ShenYiChou on 2016/12/23.
 */
@Service
public class AnnualInformService {
    @Autowired
    private AnnualInformMapper annualInformMapper;

    @Autowired
    private AnnualRestLogMapper annualRestLogMapper;

    @Autowired
    private AnnualHistoryMapper annualHistoryMapper;

    @Autowired
    private WorkExpMapper workExpMapper;

    @Autowired
    private HonorMapper honorMapper;
    //年检表彰获取
    public List<Honor> getHonors(String id_num){
        List<Honor> honorList=null;
        Example example=new Example(Honor.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(id_num)){
            criteria.andEqualTo("id_num",id_num);
        }
        honorList = honorMapper.selectByExample(example);
        return honorList;
    }

    //年检信息获取
    public List<AnnualHistory> getAnnualHistory(String ca_num){
        List<AnnualHistory> annualHistoryList=null;
        Example example=new Example(AnnualHistory.class);
        example.setOrderByClause("post_time DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(ca_num)){
            criteria.andEqualTo("ca_num",ca_num);
        }
        annualHistoryList = annualHistoryMapper.selectByExample(example);
        return annualHistoryList;
    }
    //工作经历数据库操作
    public List<WorkExp> getWorkExpsByCID(String id_num){
        List<WorkExp> workExps=null;
        Example example=new Example(WorkExp.class);
        example.setOrderByClause("start_date DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(id_num)){
            criteria.andEqualTo("id_num",id_num);
        }
        workExps = workExpMapper.selectByExample(example);
        return workExps;
    }

    public boolean newWorkExp(WorkExp workExp) {
        return workExpMapper.insertSelective(workExp)>0;
    }

    public boolean updateWorkExp(WorkExp workExp) {
        return workExpMapper.updateByPrimaryKey(workExp)>0;
    }

    public boolean deleteWorkExpById(int id) {
        return workExpMapper.deleteByPrimaryKey(id)>0;
    }

    public List<AnnualInform> getAnnualInformPage(AnnualInform annualInform) {
        Page<?> page = PageHelper.startPage(annualInform.getPage(), annualInform.getRows());
        List<AnnualInform> annualInforms=null;
        annualInforms = this.getAnnualInform(annualInform);
        annualInform.setTotalCount(page.getTotal());
        return annualInforms;
    }

    public boolean newAnnualInform(AnnualInform annualInform) {
        return annualInformMapper.insertSelective(annualInform)>0;
    }

    public boolean updateAnnualInform(AnnualInform annualInform) {
        return annualInformMapper.updateByPrimaryKey(annualInform)>0;
    }

    public boolean deleteAnnualInformById(int id) {
    //删除时需要判断为不为空
        return annualInformMapper.deleteByPrimaryKey(id)>0;
    }
    //年检初始化
    public boolean annualInitial(){
        Example example=new Example(AnnualInform.class);
        example.setOrderByClause("id DESC");
        List<AnnualInform> annualInforms= annualInformMapper.selectByExample(example);
        for(AnnualInform annualInform:annualInforms) {
            AnnualHistory annualHistory = new AnnualHistory();
            if (annualInform.getCa_stat() != 3) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date();
                String str = sdf.format(date);
                annualHistory.setPost_time(str);
                annualHistory.setAnnual_stat(annualInform.getAnnual_stat());
                annualHistory.setCa_num(annualInform.getCa_num());
                annualInform.setBf_annual_stat(annualInform.getAnnual_stat());
                annualInform.setAnnual_stat(1);
            }
            boolean flag = annualHistoryMapper.insertSelective(annualHistory)>0&&
                    annualInformMapper.updateByPrimaryKey(annualInform)>0;
            if(!flag){
                return false;
            }
        }
        return true;
    }
    //年检End
    public boolean annualEnd(){
        Example example=new Example(AnnualInform.class);
        example.setOrderByClause("id DESC");
        List<AnnualInform> annualInforms= annualInformMapper.selectByExample(example);
        for(AnnualInform annualInform:annualInforms) {
            AnnualHistory annualHistory = new AnnualHistory();
            //年检结束时，未年检和未审核都置为不通过，证书状态根据上次年检状态置为暂停和注销
            if (annualInform.getAnnual_stat() == 1||annualInform.getAnnual_stat() == 2) {
                annualInform.setAnnual_stat(4);
                if(annualInform.getBf_annual_stat()==4){
                    annualInform.setCa_stat(3);
                }else if(annualInform.getBf_annual_stat()==3){
                    annualInform.setCa_stat(2);
                }
            }
            boolean flag = annualInformMapper.updateByPrimaryKey(annualInform)>0;
            if(!flag){
                return false;
            }
        }
        return true;
    }
    //2 = Ca Pause, 3 = Ca Cancel
    public int CaCancelOrNot(int id){
        AnnualInform annualInform = annualInformMapper.selectByPrimaryKey(id);
        if(annualInform.getBf_annual_stat()==4){
            return 3;
        }
        return 2;
    }

    //import
//    public void insertAnnualList(List<AnnualInform> list) {
//        annualInformMapper.insertAnnualList(list);
//    }
    public List<AnnualInform> getEmail(){
        Example example=new Example(AnnualInform.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andIsNotNull("e_mail");
        criteria.andNotEqualTo("e_mail","");
        return annualInformMapper.selectByExample(example);
    }

    public List<AnnualInform> getAnnualInform(AnnualInform annualInform){
        List<AnnualInform> annualInforms=null;
        Example example=new Example(AnnualInform.class);
        example.setOrderByClause("case when annual_stat=2 then 2 else 1 end");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(annualInform.getName())){
            criteria.andLike("name","%"+annualInform.getName()+"%");
        }
        if(annualInform.getOrg_type()==1||annualInform.getOrg_type()==2||annualInform.getOrg_type()==3){
            criteria.andEqualTo("org_type",annualInform.getOrg_type());
        }
        if(!StringUtils.isEmpty(annualInform.getOrg_name())){
            criteria.andLike("org_name","%"+annualInform.getOrg_name()+"%");
        }
        if(annualInform.getId_type()==1||annualInform.getId_type()==2||annualInform.getId_type()==3||annualInform.getId_type()==4){
            criteria.andEqualTo("id_type",annualInform.getId_type());
        }
        if(!StringUtils.isEmpty(annualInform.getId_num())){
            criteria.andEqualTo("id_num",annualInform.getId_num());
        }
        if(!StringUtils.isEmpty(annualInform.getCa_num())){
            criteria.andEqualTo("ca_num",annualInform.getCa_num());
        }
        if(annualInform.getCa_stat()==1||annualInform.getCa_stat()==2||annualInform.getCa_stat()==3){
            criteria.andEqualTo("ca_stat",annualInform.getCa_stat());
        }
        if(annualInform.getAnnual_stat()==1||annualInform.getAnnual_stat()==2||annualInform.getAnnual_stat()==3||annualInform.getAnnual_stat()==4){
            criteria.andEqualTo("annual_stat",annualInform.getAnnual_stat());
        }
        if(annualInform.getBf_annual_stat()==1||annualInform.getBf_annual_stat()==2||annualInform.getBf_annual_stat()==3||annualInform.getBf_annual_stat()==4){
            criteria.andEqualTo("bf_annual_stat",annualInform.getBf_annual_stat());
        }
        if(Objects.equals(annualInform.getCred_record(), "1")){
            criteria.andIsNotNull("cred_record");
        }
        //转为定时任务设置
        if(!StringUtils.isEmpty(annualInform.getBirth_date())){
            criteria.andLike("birth_date","%"+annualInform.getBirth_date());
        }
        if(!StringUtils.isEmpty(annualInform.getMobile())){
            criteria.andEqualTo("mobile",annualInform.getMobile());
        }
        if(!StringUtils.isEmpty(annualInform.getE_mail())){
            criteria.andLike("e_mail","%"+annualInform.getE_mail()+"%");
        }
        if(annualInform.getPos_type()==1||annualInform.getPos_type()==2||annualInform.getPos_type()==3||annualInform.getPos_type()==4
                ||annualInform.getPos_type()==5||annualInform.getPos_type()==6||annualInform.getPos_type()==7||annualInform.getPos_type()==8){
            criteria.andEqualTo("pos_type",annualInform.getPos_type());
        }
        if(!StringUtils.isEmpty(annualInform.getDeal_type())){
            criteria.andLike("deal_type","%"+annualInform.getDeal_type()+"%");
        }
        if(annualInform.getSex()==1||annualInform.getSex()==2){
            criteria.andEqualTo("sex",annualInform.getSex());
        }
        if(annualInform.getDept_type()==1||annualInform.getDept_type()==2||annualInform.getDept_type()==3
                ||annualInform.getDept_type()==4||annualInform.getDept_type()==5){
            criteria.andEqualTo("dept_type",annualInform.getDept_type());
        }
        if(!StringUtils.isEmpty(annualInform.getDept())){
            criteria.andLike("dept","%"+annualInform.getDept()+"%");
        }
        if(annualInform.getDuty_type()==1||annualInform.getDuty_type()==2||annualInform.getDuty_type()==3
                ||annualInform.getDuty_type()==4||annualInform.getDuty_type()==5){
            criteria.andEqualTo("duty_type",annualInform.getDuty_type());
        }
        if(!StringUtils.isEmpty(annualInform.getDuty())){
            criteria.andLike("duty","%"+annualInform.getDuty()+"%");
        }
        annualInforms = annualInformMapper.selectByExample(example);
        return annualInforms;
    }

    public List<AnnualInform> getAnnualInformRest(AnnualInform annualInform){
        List<AnnualInform> annualInforms=null;
        Example example=new Example(AnnualInform.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(annualInform.getName())){
            criteria.andEqualTo("name",annualInform.getName());
        }
        if(!StringUtils.isEmpty(annualInform.getId_num())){
            criteria.andEqualTo("id_num",annualInform.getId_num());
        }
        if(!StringUtils.isEmpty(annualInform.getCa_num())){
            criteria.andEqualTo("ca_num",annualInform.getCa_num());
        }
        annualInforms = annualInformMapper.selectByExample(example);
        if(!StringUtils.isEmpty(annualInform.getWechat_name())){
            for(AnnualInform item:annualInforms) {
                item.setWechat_name(annualInform.getWechat_name());
                this.annualInformMapper.updateByPrimaryKey(item);
            }
        }
        return annualInforms;
    }

    private boolean newAnnualRestUpdateLog(AnnualRestUpdateLog annualRestUpdateLog) {
        return annualRestLogMapper.insertSelective(annualRestUpdateLog)>0;
    }
    //log还没补充完全
    public boolean setAnnualRestUpdateLog(AnnualInform annualInform){
        AnnualInform annualInformOld = new AnnualInform();
        AnnualInform annualInformSearch = new AnnualInform();
        AnnualRestUpdateLog annualRestUpdateLog = new AnnualRestUpdateLog();
        annualInformSearch.setId(annualInform.getId());
        annualInformOld = this.annualInformMapper.selectByPrimaryKey(annualInformSearch);

        List<WorkExp> workExpListOld = annualInformOld.getWorkExpList();
        List<WorkExp> workExpListNew = annualInform.getWorkExpList();
        if (!(workExpListOld.containsAll(workExpListNew)&&workExpListNew.containsAll(workExpListOld))){
            annualRestUpdateLog.setWorkExpList(workExpListOld.toString()+"==>"+workExpListNew.toString());
        }

        if("".equals(annualRestUpdateLog.toString())){
            return false; //前台没有修改的标志
        }else {
            return newAnnualRestUpdateLog(annualRestUpdateLog);//前台有修改的标志
        }
    }
}
